package com.lighting.common.swagger;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

@Configuration
@ComponentScan(basePackages = "com.lighting.common.swagger")
public class SwaggerConfiguration implements WebMvcConfigurer {

    @Value("${server.port:8080}")
    private int port;
    @Value("${lighting.common.swagger.title:swagger-bootstrap-ui RESTful APIs}")
    private String title;
    @Value("${lighting.common.swagger.basePackage:com.lighting}")
    private String basePackage;

    @Bean
    public Docket createApi() {
        ApiInfo apiInfo = new ApiInfoBuilder()
                .title(title)
                .description("swagger-bootstrap-ui")
                .version("1.0")
                .termsOfServiceUrl("http://localhost:" + port + "/")
                .build();
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo)
                .select()
                .apis(RequestHandlerSelectors.basePackage(basePackage))
                .paths(PathSelectors.any())
                .build();
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/doc.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
    }
}
